﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.XWLW;
using StateGrid.GlobalVariables;

public partial class AddFL : System.Web.UI.Page
{
    private XWLW_DAO xwlwDAO = new XWLW_DAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        if (!this.IsPostBack)
        {
            this.BindData();
        }  
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        //获取分类编码
        this.flbmHiddenField.Value = (String)Request["parent"];
        if (null == this.flbmHiddenField.Value || "".Equals(this.flbmHiddenField.Value))
        {
            String message = "没有获取到上级分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        this.flbmTextBox.Text = this.flbmHiddenField.Value;
        //this.flbmHiddenField.Value = flbm;

    }
    protected void submitButton_Click(object sender, EventArgs e)
    {
        //验证分类名称是否有效
        if (!ValidateNR())
        {
            return;
        }
            //验证分类编码是否有效
        else if (!ValidateFLBM())
        {
            return;
        }
        //验证通过则插入新记录
        XWLW_FL xwlwFL = new XWLW_FL();
        xwlwFL.nr = this.nrTextBox.Text.Trim();
        xwlwFL.flbm = this.flbmTextBox.Text.Trim();
        xwlwFL.fltx = this.fltxTextBox.Text.Trim();
        xwlwFL.bz = this.fltxTextBox.Text.Trim();
        xwlwDAO.InsertXWLW_FL(xwlwFL);

        Response.Write("<Script>alert('添加学位论文分类成功')</Script>");
    }

    /// <summary>
    /// 验证分类编码
    /// </summary>
    private Boolean ValidateFLBM()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";
        if (this.flbmTextBox.Text.Length != this.flbmHiddenField.Value.Length + 2)
        {
            this.errorLabel.Text = "错误：分类编码位数不正确";
            flag = false;
        }
        else
        {
            if (!this.flbmTextBox.Text.StartsWith(this.flbmHiddenField.Value))
            {
                this.errorLabel.Text = "错误：分类编码不正确";
                flag = false;
            }
            else
            {
                XWLW_FL xwlwFL = this.xwlwDAO.GetXWLW_FLByFlbm(this.flbmTextBox.Text.Trim());
                if (null != xwlwFL)
                {
                    this.errorLabel.Text = "错误：该分类编码已存在";
                    flag = false;
                }
            }
        }

        return flag;
    }

    /// <summary>
    /// 验证内容
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateNR()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";

        if (null == this.nrTextBox.Text || "".Equals(this.nrTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：内容不能为空";
            flag = false;
        }

       XWLW_FL xwlwFL = xwlwDAO.GetXWLW_FLByNr(this.nrTextBox.Text.Trim());

        if (null != xwlwFL)
        {
            this.errorLabel.Text = "错误：该内容已存在";
            flag = false;
        }
        return flag;
    }
    
    /// <summary>
    /// 恢复分类编码初始信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.flbmTextBox.Text = this.flbmHiddenField.Value;
    }
}